package com.zhiqian.community.controller;

import com.zhiqian.community.entity.Page;
import com.zhiqian.community.entity.User;
import com.zhiqian.community.service.FollowService;
import com.zhiqian.community.service.UserService;
import com.zhiqian.community.util.CommunityConstant;
import com.zhiqian.community.util.CommunityUtil;
import com.zhiqian.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FollowController implements CommunityConstant {
    @Autowired
    private FollowService followService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private UserService userService;

    /**
     * 关注或取消关注
     * @param entityType
     * @param entityId
     * @return
     */
    @PostMapping("/follow")
    @ResponseBody
    public String follow(int entityType,Integer entityId){
        User user = hostHolder.getUser();
        if (user==null){
            return CommunityUtil.getJSONString(1,"操作失败");
        }
        if (ENTITY_TYPE_USER==entityType&&user.getId().equals(entityId)){
            return CommunityUtil.getJSONString(1,"不能关注自己哦");
        }
        followService.doFollow(entityType, entityId, user.getId());
        int followStatus = followService.selectFollowStatus(entityType, entityId, user.getId());
        long followerCount = followService.selectFollowerCount(entityType, entityId);
        Map<String, Object> result = new HashMap<>(1);
        result.put("followStatus",followStatus);
        result.put("followerCount",followerCount);
        return CommunityUtil.getJSONString(0,"操作成功!",result);
    }

    @GetMapping("/followerDetail/{entityType}/{entityId}")
    public String followerDetail(@PathVariable("entityType") int entityType, @PathVariable("entityId") Integer entityId, Page page, Model model){
        User target = userService.getUserById(entityId);
        if (target==null){
            throw new RuntimeException("该用户不存在");
        }
        page.setRows((int) followService.selectFollowerCount(entityType,entityId));
        page.setPath("/followerDetail/"+entityType+"/"+entityId);
        page.setLimit(20);
        List<Map<String, Object>> result = followService.selectFollowerList(entityType, entityId,hostHolder.getUser().getId(),page);
        model.addAttribute("followerList",result);
        model.addAttribute("target",target);
        return "/site/follower";
    }

    @GetMapping("/followeeDetail/{targetId}")
    public String followeeDetail(@PathVariable("targetId") Integer targetId,Page page,Model model){
        User target = userService.getUserById(targetId);
        if (target==null){
            throw new RuntimeException("该用户不存在");
        }
        page.setRows((int) followService.selectFolloweeCount(ENTITY_TYPE_USER,targetId));
        page.setPath("/followeeDetail/"+targetId);
        page.setLimit(20);
        List<Map<String, Object>> result = followService.selectFolloweeList(targetId, hostHolder.getUser().getId(), page);
        model.addAttribute("followeeList",result);
        model.addAttribute("target",target);
        return "/site/followee";
    }
}
